package http.DataUtil.text;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Vector;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.KeyEvent;

public class TextUtil {
	int m_iTextPosX;
	int m_iTextPosY;
	int m_iTextWidth;
	int m_iTextHeight;
	int m_iFontHeight;
	int m_ipageLineNum;
	int m_iTextBGColor;
	int m_iTextColor;
	int m_iAlpha;
	int m_iRealLine;
	int m_iCurLine;
	String m_strText;
	Vector<String> m_String;
	Paint m_paint;
	int m_iTextSize;
	static int CRYPTKEY = 90;

	public static byte[] CancelHead(byte[] bytes) {
		if (new String(bytes).indexOf("flystm!") != -1) {
			int iHeaderLength = 0;
			try {
				iHeaderLength = "flystm!".getBytes("utf-8").length;
			} catch (UnsupportedEncodingException localUnsupportedEncodingException) {
			}
			byte[] bodytemp = new byte[bytes.length - iHeaderLength];
			System.arraycopy(bytes, iHeaderLength, bodytemp, 0, bytes.length
					- iHeaderLength);
			return bodytemp;
		}
		return bytes;
	}

	public static int innerStrings(String str, String[] strs) {
		for (int i = 0; i < strs.length; i++) {
			if (strs[i].equals(str)) {
				return 1;
			}
		}

		return -1;
	}

	public static int innerIgnoreCaseStrings(String str, String[] strs) {
		for (int i = 0; i < strs.length; i++) {
			if (strs[i].equalsIgnoreCase(str)) {
				return 1;
			}
		}

		return -1;
	}

	public static byte[] compressData(byte[] bytes) {
		ByteArrayOutputStream bis = new ByteArrayOutputStream();
		try {
			byte[] tempByte = new byte[100];
			int compressedDataLength = -1;
			Deflater compresser = new Deflater();
			compresser.setInput(bytes);
			compresser.finish();
			while (compressedDataLength != 0) {
				compressedDataLength = compresser.deflate(tempByte);
				bis.write(tempByte, 0, compressedDataLength);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return bis.toByteArray();
	}

	public static byte[] decompressData(byte[] bytes)
			throws DataFormatException, IOException {
		ByteArrayOutputStream bis = new ByteArrayOutputStream();

		int resultLength = -1;
		try {
			Inflater decompresser = new Inflater();
			decompresser.setInput(bytes, 0, bytes.length);
			byte[] result = new byte[1024];
			while (resultLength != 0) {
				resultLength = decompresser.inflate(result);
				bis.write(result, 0, resultLength);
			}
			decompresser.end();
			result = (byte[]) null;
			decompresser = null;
		} catch (Exception localException) {
		}
		return bis.toByteArray();
	}

	public static byte[] decrypt(byte[] bytes) {
		int len = bytes.length;
		for (int i = 0; i < len; i++) {
			bytes[i] = (byte) (bytes[i] ^ CRYPTKEY);
		}

		return bytes;
	}

	public static byte[] ecrypt(byte[] bytes) {
		int len = bytes.length;
		for (int i = 0; i < len; i++) {
			bytes[i] = (byte) (bytes[i] ^ CRYPTKEY);
		}
		return bytes;
	}

	public static byte[] ecryptAfterCompressData(byte[] bytes) {
		return ecrypt(compressData(bytes));
	}

	public static byte[] decompressAfterdecrypt(byte[] bytes) throws Exception {
		if ((bytes == null) || (bytes.length == 0))
			return null;
		return decompressData(decrypt(bytes));
	}

	public TextUtil() {
		this.m_paint = new Paint();
		this.m_String = new Vector<String>();
	}

	public TextUtil(String strText, int x, int y, int w, int h, int bgcolor,
			int txetcolor, int a, int iTextSize) {
		this.m_paint = new Paint();
		this.m_String = new Vector<String>();

		this.m_strText = strText;

		this.m_iTextPosX = x;
		this.m_iTextPosY = y;
		this.m_iTextWidth = w;
		this.m_iTextHeight = h;

		this.m_iTextBGColor = bgcolor;
		this.m_iTextColor = txetcolor;

		this.m_iTextSize = iTextSize;

		this.m_iAlpha = a;
	}

	public void DrawText(Canvas canvas) {
		int i = this.m_iCurLine;
		for (int j = 0; i < this.m_iRealLine; j++) {
			if (j > this.m_ipageLineNum) {
				break;
			}
			canvas.drawText((String) this.m_String.elementAt(i),
					this.m_iTextPosX,
					this.m_iTextPosY + this.m_iFontHeight * j, this.m_paint);

			i++;
		}
	}

	public void GetTextIfon() {
		int w = 0;
		int istart = 0;
		Paint.FontMetrics fm = this.m_paint.getFontMetrics();

		this.m_iFontHeight = ((int) Math.ceil(fm.descent - fm.top) + 2);

		this.m_ipageLineNum = (this.m_iTextHeight / this.m_iFontHeight);

		for (int i = 0; i < this.m_strText.length(); i++) {
			char ch = this.m_strText.charAt(i);
			float[] widths = new float[1];
			String srt = String.valueOf(ch);
			this.m_paint.getTextWidths(srt, widths);

			if (ch == '\n') {
				this.m_iRealLine += 1;
				this.m_String.addElement(this.m_strText.substring(istart, i));
				istart = i + 1;
				w = 0;
			} else {
				w += (int) Math.ceil(widths[0]);
				if (w > this.m_iTextWidth) {
					this.m_iRealLine += 1;
					this.m_String.addElement(this.m_strText
							.substring(istart, i));
					istart = i;
					i--;
					w = 0;
				} else if (i == this.m_strText.length() - 1) {
					this.m_iRealLine += 1;
					this.m_String.addElement(this.m_strText.substring(istart,
							this.m_strText.length()));
				}
			}
		}
	}

	public void InitText(String strText, int x, int y, int w, int h,
			int bgcolor, int txetcolor, int a, int iTextSize) {
		this.m_iCurLine = 0;
		this.m_ipageLineNum = 0;
		this.m_iRealLine = 0;
		this.m_strText = "";
		this.m_iTextPosX = 0;
		this.m_iTextPosY = 0;
		this.m_iTextWidth = 0;
		this.m_iTextHeight = 0;
		this.m_iTextBGColor = 0;
		this.m_iTextColor = 0;
		this.m_iTextSize = 0;
		this.m_iAlpha = 0;

		this.m_String.clear();

		SetText(strText);
		SetRect(x, y, w, h);
		SetBGColor(bgcolor);
		SetTextColor(txetcolor);
		SetFontSize(iTextSize);
		SetAlpha(a);

		SetPaint();

		GetTextIfon();
	}

	public boolean KeyDown(int keyCode, KeyEvent event) {
		if (keyCode == 19) {
			if (this.m_iCurLine > 0)
				this.m_iCurLine -= 1;
		} else if ((keyCode == 20)
				&& (this.m_iCurLine + this.m_ipageLineNum < this.m_iRealLine - 1)) {
			this.m_iCurLine += 1;
		}

		return false;
	}

	public void SetAlpha(int a) {
		this.m_iAlpha = a;
	}

	public void SetBGColor(int bgcolor) {
		this.m_iTextBGColor = bgcolor;
	}

	public void SetFontSize(int iTextSize) {
		this.m_iTextSize = iTextSize;
	}

	public void SetPaint() {
		this.m_paint.setARGB(this.m_iAlpha, Color.red(this.m_iTextColor),
				Color.green(this.m_iTextColor), Color.blue(this.m_iTextColor));
		this.m_paint.setTextSize(this.m_iTextSize);
	}

	public void SetRect(int x, int y, int w, int h) {
		this.m_iTextPosX = x;
		this.m_iTextPosY = y;
		this.m_iTextWidth = w;
		this.m_iTextHeight = h;
	}

	public void SetText(String strText) {
		this.m_strText = strText;
	}

	public void SetTextColor(int txetcolor) {
		this.m_iTextColor = txetcolor;
	}

	public static boolean isEmpty(String name) {
		return (name == null) || (name.trim().length() == 0);
	}
}